package com.example.sdcardfiletest;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.TextureView;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {
	public static final int MODE= Context.MODE_WORLD_READABLE+Context.MODE_WORLD_WRITEABLE;
    public static final String PREFERENCE_NAME="WX_SETING";
	
	TextView tv1,tv2;
	EditText et1,et2;
	Button logBtn;
    CheckBox cb1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv1=(TextView) findViewById(R.id.textView1);
        tv2=(TextView) findViewById(R.id.textView2);
        et1=(EditText) findViewById(R.id.editText1);
        et2=(EditText) findViewById(R.id.editText2);
        cb1=(CheckBox) findViewById(R.id.checkBox1);
        logBtn=(Button) findViewById(R.id.button1);
        
        logBtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(cb1.isChecked()){
					saveSharedPreference();
				}
			  Intent intent=new Intent(MainActivity.this,NewActivity.class);
			  startActivity(intent);
			}
		});
        
    }
    private void saveSharedPreference(){
    	SharedPreferences sp=getSharedPreferences(PREFERENCE_NAME, MODE);
    	SharedPreferences.Editor editor=sp.edit();
    	editor.putString("username", et1.getText().toString());
    	editor.putString("password", et2.getText().toString());
    	editor.commit();
    }
    private void loadSharedPreference(){
        SharedPreferences sh=getSharedPreferences(PREFERENCE_NAME,MODE);
        String username = sh.getString("username","");
        et1.setText(username);
        String password = sh.getString("password","");
        et2.setText(password);
 }
    @Override
   	protected void onStart() {
   		// TODO Auto-generated method stub
   		super.onStart();
   		loadSharedPreference();
   	}
}
